<?php


namespace app\api\controller\v13;


use app\common\cachemodel\ArticleRead;
use app\common\lib\exception\ApiException;
use app\common\model\Article as ArticleModel;
use app\common\model\ArticleCategory;
use app\common\model\ArticleComment;
use app\common\model\ArticleCommentReply;
use app\common\model\Recode as RecodeModel;
use Exception;
use think\response\Json;

class Article extends Auth2
{
    /**
     * 获取文章分类
     * @return Json
     * @throws ApiException
     */
    public function category()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', '', 403);
        }

        $category = (new ArticleCategory())->getArticleCategory();

        return show(config('code.success'), '文章分类加载成功', $category);
    }

    /**
     * @title 获取文章列表
     * @method GET
     * @return array|Json
     * @throws Exception
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', '', 403);
        }

        $map = [
            'page' => input('get.page/d', 1),
            'cid' => input('get.cid/d'),
            'uid' => $this->user['id'] ?? null,
            'uc_id' => $this->user['uc_id'],
        ];

        $data = (new ArticleModel())->getArticleList($map);

        return show(config('code.success'), '加载成功', $data);
    }

    /**
     * 查看文章详情 - 已加缓存
     * @return Json
     * @throws \think\Exception
     */
    public function article_details()
    {
        $map = [
            'id' => input('param.id/d', 0),
            'uid' => $this->user['id'] ?? null,
            'ucid' => $this->user['uc_id'] ?? null,
            'is_old' => false,
        ];
        $data = (new ArticleModel())->getArticleDetails($this->user['id'], $map);
        if ($data)
        {
            if ($this->user)
            {
                //内容已读
                (new ArticleRead())->saveData((int)$this->user['id'], 1, $map['id']);
            }
            return show(config('code.success'), '加载成功', $data);
        }
        return show(config('code.error'), '内容不存在');
    }

    /**
     * 评论列表
     * @return Json
     * @throws Exception
     */
    public function comment_lists()
    {
        $aid = input('param.aid/d');
        $page = input('param.page/d', 1);
        // 缓存
        $data_list = (new ArticleComment())->getComments([
            'aid' => $aid,
            'page' => $page,
            'uc_id' => $this->user['uc_id'] ?? null,
            'uid' => $this->user['id'] ?? null,
        ]);

        return show(config('code.success'), '操作成功', $data_list);
    }

    /**
     * 查看全部回复
     * @return Json
     */
    public function reply_lists()
    {
        $commentid = input('param.commentid/d');
        $page = input('param.page/d', 1);
        // 缓存
        $data = (new ArticleCommentReply())->getReplyLists(['ac_id' => $commentid], $page);
        return show(200, 'ok', $data);
    }

    /**
     * 周运详情
     * @return Json
     * @throws ApiException
     */
    public function fortune_week_detail()
    {
        $in_sign = input('param.in_sign/d', 0);
        //若用户登陆了且有档案则，获取用户的落座
        if (!$in_sign && $this->user && $this->user['rid'])
        {
            $map = ['uid' => $this->user['id'], 'isself' => 1];
            $recode_info = (new RecodeModel())->getSelfRecode($map);

            if ((!$recode_info || !$recode_info['sun']))
            {
                return show(0, '档案缺失请添加档案');
            }
            $in_sign = explode('-', $recode_info['sun'])[0];
        } elseif (!in_array($in_sign, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], true))
        {
            return show(0, '星座未知');
        }

        $data = (new ArticleModel())->getFortune('week', $in_sign);

        return show(config('code.success'), '加载成功', $data);
    }

    /**
     * 月运详情
     * @return Json
     * @throws ApiException
     */
    public function fortune_month_detail()
    {
        $in_sign = input('param.in_sign/d');
        if (!isset($in_sign) || !$in_sign || !in_array($in_sign, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], true))
        {
            return show(config('code.error'), '星座未知');
        }

        $data = (new ArticleModel())->getFortune('month', $in_sign);

        return show(config('code.success'), '加载成功', $data);
    }

    /**
     * 年运详情
     * @return Json
     * @throws ApiException
     */
    public function fortune_year_detail()
    {
        $in_sign = input('param.in_sign/d');
        if (!isset($in_sign) || !$in_sign || !in_array($in_sign, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], true))
        {
            return show(config('code.error'), '星座未知');
        }

        $data = (new ArticleModel())->getFortune('year', $in_sign);

        return show(config('code.success'), '加载成功', $data);
    }
}
